/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core;

import com.inspur.edp.bff.engine.core.exception.BffEngineCoreException;
import com.inspur.edp.bff.engine.core.exception.BffEngineCoreErrorCodes;
import com.inspur.edp.bff.engine.entity.BffEngineChildAccessor;
import com.inspur.edp.bff.engine.entity.BffEngineRootAccessor;
import com.inspur.edp.bff.engine.entity.BffEngineRootReadonlyAccessor;
import com.inspur.edp.cef.api.dataType.base.IEntityAccessorCreator;
import com.inspur.edp.cef.entity.accessor.base.IAccessor;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefEntityResInfoImpl;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import lombok.var;

public class BffEngineAccessorCreator implements IEntityAccessorCreator {

    private final CefModelResInfoImpl resInfo;
    private GspViewModel vo;

    public BffEngineAccessorCreator(GspViewModel vo, CefModelResInfoImpl resInfo) {
        this.vo = vo;
        this.resInfo = resInfo;
    }

    @Override
    public IAccessor createChildAccessor(String nodeCode, ICefData data) {
        if("RootNode".equals(nodeCode))
            return new BffEngineRootAccessor(vo.getMainObject(), (IEntityData)data,
                    getResInfo(vo.getMainObject().getCode()));

        for (var obj :
                vo.getAllObjectList()) {
            if(!nodeCode.equals(obj.getCode()))
                continue;
            if(obj.getCode().equals(vo.getMainObject().getCode()))
                return new BffEngineRootAccessor(vo.getMainObject(), (IEntityData)data, getResInfo(obj.getCode()));
            return new BffEngineChildAccessor(obj, (IEntityData)data, getResInfo(obj.getCode()));
        }

        throw new BffEngineCoreException(BffEngineCoreErrorCodes.GSP_BFF_ENGINE_1002,vo.getName(),nodeCode);
    }

    @Override
    public IAccessor createChildReadonlyAccessor(String nodeCode, ICefData data) {
        //TODO
        return null;
    }

    @Override
    public IAccessor createAccessor(ICefData data) {
        return new BffEngineRootAccessor(vo.getMainObject(), (IEntityData)data,
                getResInfo(vo.getMainObject().getCode()));
    }

    @Override
    public IAccessor createReadonlyAccessor(ICefData data) {
        return new BffEngineRootReadonlyAccessor(vo.getMainObject(), (IEntityData)data);
    }

    private CefEntityResInfoImpl getResInfo(String nodeCode) {
        return (CefEntityResInfoImpl) resInfo.getCustomResource(nodeCode);
    }
}
